package com.tea.admin.modules.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tea.admin.common.annotation.Permission;
import com.tea.common.entity.Carousel;
import com.tea.common.result.CommonPage;
import com.tea.common.result.CommonResult;
import com.tea.common.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 轮播图管理Controller
 */
@RestController
@RequestMapping("/api/app/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 分页查询轮播图列表
     */
    @GetMapping("/page")
    @Permission("system:app:carousel")
    public CommonResult<CommonPage<Carousel>> page(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "status", required = false) Integer status) {
        Page<Carousel> page = new Page<>(pageNum, pageSize);
        Page<Carousel> result = carouselService.pageCarousel(page, title, status);
        return CommonResult.success(CommonPage.restPage(result));
    }

    /**
     * 获取轮播图详情
     */
    @GetMapping("/{id}")
    @Permission("system:app:carousel")
    public CommonResult<Carousel> getById(@PathVariable Long id) {
        Carousel carousel = carouselService.getById(id);
        if (carousel == null) {
            return CommonResult.failed("轮播图不存在");
        }
        return CommonResult.success(carousel);
    }

    /**
     * 添加轮播图
     */
    @PostMapping
    @Permission("system:app:carousel")
    public CommonResult<Boolean> add(@RequestBody Carousel carousel) {
        boolean success = carouselService.save(carousel);
        return success ? CommonResult.success(true, "添加成功") : CommonResult.failed("添加失败");
    }

    /**
     * 修改轮播图
     */
    @PutMapping
    @Permission("system:app:carousel")
    public CommonResult<Boolean> update(@RequestBody Carousel carousel) {
        boolean success = carouselService.updateById(carousel);
        return success ? CommonResult.success(true, "修改成功") : CommonResult.failed("修改失败");
    }

    /**
     * a 轮播图
     */
    @DeleteMapping("/{id}")
    @Permission("system:app:carousel")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        boolean success = carouselService.removeById(id);
        return success ? CommonResult.success(true, "删除成功") : CommonResult.failed("删除失败");
    }

    /**
     * 更新轮播图状态
     */
    @PutMapping("/{id}/status/{status}")
    @Permission("system:app:carousel")
    public CommonResult<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean success = carouselService.updateStatus(id, status);
        return success ? CommonResult.success(true, "状态更新成功") : CommonResult.failed("状态更新失败");
    }
} 